package com.orangelife.mobile.address.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.common.sp.ShoppingSp;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.address.adapter.AddressListAdapter;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MakeAddressListActivity extends OrangeLifeBaseActivity {

    private TextView title;
    private ListView addresslist;
    private List<Map<String, Object>> listMap;
    private static final int REQUEST_CODE = 1;
    private AddressListAdapter<Map<String, Object>> adapter;
    private Dialog dlgLoading;
    private Button saveAddress;
    private LinearLayout addressManage;
    /** 判断是否跳转到登录页，0：没有，1：有 */
    private int isTurnToLogin = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_address_list);
        initView();
        findView();
    }

    private void initView() {
        title = (TextView) findViewById(R.id.tv_title);
        addresslist = (ListView) findViewById(R.id.lv_address);
        saveAddress = (Button) findViewById(R.id.bt_address);
        addressManage = (LinearLayout) findViewById(R.id.ll_addressManage);
    }

    /**
     * 设置适配器
     */
    private void setAdapter() {
        listMap = new ArrayList<>();
        adapter = new AddressListAdapter<>(
                this, listMap);
        addresslist.setAdapter(adapter);
        addresslist.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {
                return false;
            }
        });
    }

    private void findView() {
        title.setText("管理地址");
        setAdapter();
        addresslist.setOnItemClickListener(itemClickLintener);
        addressManage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                startActivity(new Intent(MakeAddressListActivity.this, AddressListActivity.class));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        closeActivity();
        getAddressList();
    }

    private void getAddressList() {
        dlgLoading = DialogHelper.getInstance().createLoadingDialog(this,
                Constant.LOADING);
        dlgLoading.show();
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, Constant.ADDRESS_QUERY_URL, null, handler, 0, HttpUtil.ACTION_URI);

    }

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 500:
                    ToastHelper.getInstance()._toast(msg.obj.toString());
                    DialogHelper.closeDialog(dlgLoading);
                    break;
                case 1011:
                    if (isTurnToLogin == 0) {
//					ToastHelper.getInstance()._toast(msg.obj.toString());
                        isLogin(dlgLoading);
                        isTurnToLogin = 1;
                    }
                    break;
                case 0:
                    listMap = JsonReponseHandler.getListFromJson(msg.obj.toString(), adapter, dlgLoading);
                    if (listMap.size() > 0)
                        saveAddress.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
            if (dlgLoading != null)
                dlgLoading.dismiss();
        }

    };


    AdapterView.OnItemClickListener itemClickLintener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> aview, View view, int position,
                                long arg3) {
            Intent intent = new Intent();
            Map<String, Object> map = listMap.get(position);
            Bundle bundle = new Bundle();
            bundle.putSerializable("listmap", (Serializable) map);
            intent.putExtras(bundle);
            setResult(Activity.RESULT_FIRST_USER, intent);
            ShoppingSp.setTradeAddressInfo(
                    String.valueOf(map.get("consignee")),
                    String.valueOf(map.get("phone")),
                    String.valueOf(map.get("addressJoin")),
                    String.valueOf(map.get("addressID")),
                    String.valueOf(map.get("provinceCode")),
                    String.valueOf(map.get("cityCode")),
                    String.valueOf(map.get("districtID")));
            finish();
        }
    };

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (data != null)
                    getAddressList();
                break;

            default:
                break;
        }
    }

    public void addAddress(View view) {
        Intent intent = new Intent();
        intent.setClass(MakeAddressListActivity.this, AddAddressActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onDestroy() {
        if (dlgLoading != null)
            dlgLoading.dismiss();
        super.onDestroy();
    }
}
